package com.org.manhu.chargehelper.ui;

import java.util.Map;

import com.org.manhu.chargehelper.ChargeApplication;
import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.constant.Constant;
import com.org.manhu.chargehelper.custom.BatteryCharger;
import com.org.manhu.chargehelper.tools.AppManager;
import com.org.manhu.chargehelper.tools.DeviceInfo;
import com.org.manhu.chargehelper.tools.JsonUtil;
import com.org.manhu.chargehelper.tools.KeyBoardUtils;
import com.org.manhu.chargehelper.tools.NetUtils;
import com.org.manhu.chargehelper.tools.SharedPreferncesUtil;
import com.org.manhu.chargehelper.tools.ToastUtils;
import com.org.manhu.chargehelper.webservice.AsyncProcessData;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * @文件名：ModifyNickNameActivity.java
 * @包名：com.org.manhu.chargehelper.ui
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2015年2月6日 下午5:19:15
 *
 * @描述：
 */
public class ModifyNickNameActivity extends Activity implements OnClickListener {

	private RelativeLayout userInfo;
	private EditText nickNameEdt;
	private ImageView backIv;
	private Button sureBtn, cancelBtn;
	private BatteryCharger battery;

	private AsyncProcessData request;
	private String nickName;
	private String channelId;
	private String devId;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			String json = msg.getData().getString("jsonStr");
			if (Constant.EDIT_USER_INFO.equals(msg.obj.toString())) {
				Map<String, String> feedbackMap = JsonUtil.parseAll(json);
				if (feedbackMap != null) {
					if (feedbackMap.get("Code").equals("0")) {
						clientInit();
					} else {
						Map<String, String> map = JsonUtil.parseAll(json);
						if (map != null) {
							if (map.get("Code").equals("1")) {
								ModifyNickNameActivity.this.finish();

								Intent intent = new Intent(
										Constant.ACTION_UPDATE_NICKNAME);
								intent.putExtra("nickName", nickName);
								ModifyNickNameActivity.this
										.sendBroadcast(intent);
								SharedPreferncesUtil.saveNickName(
										ModifyNickNameActivity.this, nickName);
							}
							ToastUtils.showShort(ModifyNickNameActivity.this,
									map.get("Description"));
						}
					}
				} else {
					modifyUserName();
				}
			} else if (Constant.CLIENT_INIT.equals(msg.obj.toString())) {
				Map<String, String> initMap = JsonUtil.parseAll(json);
				if (initMap != null) {
					ChargeApplication.updateInitInfo(initMap);
					modifyUserName();
				} else {
					clientInit();
				}
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_user_name);
		AppManager.getAppManager().addActivity(this);
		String hint = getIntent().getStringExtra("nickname");

		request = new AsyncProcessData();

		userInfo = (RelativeLayout) findViewById(R.id.user_info);
		battery = (BatteryCharger) findViewById(R.id.battery_charge);
		userInfo.setVisibility(View.GONE);
		nickNameEdt = (EditText) findViewById(R.id.user_nick_name_edt);
		backIv = (ImageView) findViewById(R.id.title_back);
		sureBtn = (Button) findViewById(R.id.modify_sure);
		cancelBtn = (Button) findViewById(R.id.modify_cancle);
		nickNameEdt.setHint(hint);

		if (ChargeApplication.isBoxConnected) {
			battery.setVisibility(View.VISIBLE);
			battery.startChargerAnim();
		}

		backIv.setOnClickListener(this);
		sureBtn.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.modify_sure:
			KeyBoardUtils.closeKeybord(nickNameEdt, this);
			nickName = nickNameEdt.getText().toString();
			if (TextUtils.isEmpty(nickName) || nickName.length() < 4) {
				ToastUtils.showShort(this, "昵称不符合要求，请检查！");
				return;
			}
			modifyUserName();
			break;
		case R.id.modify_cancle:
			finish();
			break;
		}
	}

	/**
	 * @描述： 客户端初始化，获取AuthKey
	 */
	private void clientInit() {
		if (NetUtils.isConnected(this)) {
			if (Constant.IS_USE_BOXINFO) {
				channelId = SharedPreferncesUtil.getBoxInfo(this,
						Constant.SP_BOX_CHANNELID);
				devId = SharedPreferncesUtil.getBoxInfo(this,
						Constant.SP_BOX_DEVID);
			} else {
				channelId = "0";
				devId = "0";
			}
			request.getRequestData(mHandler, Constant.CLIENT_INIT, "AuthKey",
					"", "Imei", DeviceInfo.getIMEI(), "Imsi",
					DeviceInfo.getIMSI(), "AndroidId",
					DeviceInfo.getAndroidID(), "PhoneTypeId",
					DeviceInfo.getDeviceModel(), "ChannelId", channelId,
					"DevId", devId);
		} else {
			ToastUtils.showShort(this, "网络异常，请稍检查网络");
		}
	}

	/**
	 * @描述：
	 */
	private void modifyUserName() {
		if (NetUtils.isConnected(this)) {
			request.getRequestData(mHandler, Constant.EDIT_USER_INFO,
					"AuthKey", ChargeApplication.getInitInfo().get("AuthKey"),
					"Assignname", nickName, "Mobile", "", "NickName", "");
		} else {
			ToastUtils.showShort(getApplicationContext(), "网络异常，请稍检查网络");
		}
	}

}
